package main

import (
	"fmt"
	"gopkg.in/ini.v1"
	"logagent/conf"
	"logagent/etcd"
	"logagent/kafka"
	"logagent/taillog"
	"sync"
	"time"
)

var cfg = new(conf.AppConf)

func main() {
	//0.加载配置文件
	err := ini.MapTo(cfg, "./conf/config.ini")
	fmt.Println(cfg)
	//1.初始化kafka
	err = kafka.Init([]string{cfg.KafkaConf.Address}, cfg.Chan_max_size)
	if err != nil {
		fmt.Println("init kafka failed:", err)
		return
	}
	fmt.Println("init kafka success")
	//2初始化etcd
	err = etcd.Init(cfg.EtcdConf.Address, time.Duration(cfg.EtcdConf.Timeout)*time.Second)
	if err != nil {
		fmt.Printf("init etcd failed err:%v", err)
	}
	fmt.Println("init etcd success")

	logEntryConf, err := etcd.GetConf(cfg.EtcdConf.Key)
	if err != nil {
		fmt.Printf("get conf from etcd failed err:%v\n", err)
	}
	fmt.Printf("get conf from etcd success ,%v\n", logEntryConf)
	//2.1拍一个哨兵去剑士日志收集项的变化
	taillog.Init(logEntryConf)           //注意：先初始化newConfChan通道下面的语句
	newConfChan := taillog.NewConfChan() //从taillog包中获取对外暴露的通道
	//3.收集日志发往kafka
	//3.1循环每一条日志手机数据创建tailobj
	wg := sync.WaitGroup{}

	wg.Add(1)
	go etcd.WatchConf(cfg.EtcdConf.Key, newConfChan)
	wg.Wait()

}
